layui.use(['customUtil'], function () {
    var customUtil = layui.customUtil;
    var statistic = {
        _constant: {
            api: {
                queryBookTypes:"/bookType/bookType/list",
                bookTypeStatistic:"/statistic/statistic/bookTypeStatistic"
            },
            bookTypes: {},
        },

        _init: function () {
            let _this = this;
            _this._bind();
            _this._initRender();
            _this._beforeRender();
        },

        _initRender:function(){
            let _this = this;
        },

        _bind: function () {
            let _this = this;
            $("#add_book_btn").on("click", function () {
                _this._addBook();
            });
        },

        _beforeRender:function(){
            let _this = this;
            customUtil.get(_this._constant.api.queryBookTypes, function (data) {
                if (data.code == "0") {
                    _this._constant.bookTypes = data.data;
                    _this._beforeRenderBookTypeCharts();
                } else {
                    layer.alert(data.messages, {
                        title: '系统异常'
                    })
                }
            });
        },

        _beforeRenderBookTypeCharts:function(){
            let _this = this;
            customUtil.get(_this._constant.api.bookTypeStatistic, function (data) {
                if (data.countResult) {
                    _this._renderBookTypeCharts(data.countResult);
                } else {
                    layer.alert(data.messages, {
                        title: '系统异常'
                    })
                }
            });
        },

        _renderBookTypeCharts:function(data){
            let _this = this;
            if(data.length == 0){
                layer.msg("暂无数据");
                return ;
            }
            let chartsData = [];
            let bookTypeList = _this._constant.bookTypes;
            for (let i = 0; i < bookTypeList.length; i++) {
                let contain = false;
                let bookItem = bookTypeList[i]
                for (let j = 0; j < data.length; j++) {
                    let item = data[j];
                    if(bookItem.bookTypeId.toString() === item.book_type_id){
                        chartsData[chartsData.length] = {
                            "name" : bookItem.bookTypeName,
                            "value" : item.Count
                        }
                        contain = true;
                        break;
                    }
                }
                if(!contain){
                    chartsData[chartsData.length] = {
                        "name" : bookItem.bookTypeName,
                        "value" : 0
                    }
                }
            }

            let option = {
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                toolbox: {
                    show : true,
                },
                  legend: {
                    type:"scroll",
                    orient: 'vertical',
                    left:'10%',
                    align:'left',
                    top:'middle',
                    textStyle: {
                        color:'#8C8C8C'
                    },
                    height:150
                },
                series : [
                    {
                        name:'类型统计',
                        type:'pie',
                        radius : [0, 150],
                        data:chartsData
                    }
                ]
            };
            var myChart = echarts.init(document.getElementById('bookTypeStatisticChart'));
            myChart.setOption(option);
        },

        _deleteBook: function (data) {
            let _this = this;
            layer.confirm('确认删除？', {
              btn: ['确定','取消'] //按钮
            }, function(){
                let param = {
                    "bookId" : data.bookId
                };
                customUtil.post(_this._constant.api.deleteBook, param, function (data) {
                    if (data.success) {
                        layer.msg(data.msg);
                        _this._renderTable();
                    } else {
                        layer.alert(data.messages, {
                            title: '操作失败'
                        })
                    }
                });
            }, function(){
                 layer.closeAll();
            });
        },
    };

    statistic._init();
});